﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace TileMapEditor.Internal
{
	public static class TileMapEditorUtility
	{
        public static void BeginLines( Color color )
        {
            HandlesUtilityWrapper.ApplyWireMaterial();

            GL.PushMatrix();
            GL.MultMatrix( Handles.matrix );
            GL.Begin(GL.LINES);
            GL.Color( color );
        }

        public static void DrawLine(Vector3 v1, Vector3 v2)
        {
            GL.Vertex( v1 );
            GL.Vertex( v2 );
        }

        public static void EndLines()
        {
            GL.End();
            GL.PopMatrix();
        }

        public static void DrawBox(Rect rect)
        {
            Vector3[] array = new Vector3[5];
            int num = 0;
            array[num++] = new Vector3( rect.xMin, rect.yMin, 0f );
            array[num++] = new Vector3( rect.xMax, rect.yMin, 0f );
            array[num++] = new Vector3( rect.xMax, rect.yMax, 0f );
            array[num++] = new Vector3( rect.xMin, rect.yMax, 0f );
            TileMapEditorUtility.DrawLine( array[0], array[1] );
            TileMapEditorUtility.DrawLine( array[1], array[2] );
            TileMapEditorUtility.DrawLine( array[2], array[3] );
            TileMapEditorUtility.DrawLine( array[3], array[0] );
        }

        public static void BeginQuads( Color color )
        {
            HandlesUtilityWrapper.ApplyWireMaterial();

            GL.PushMatrix();
            GL.MultMatrix( Handles.matrix );
            GL.Begin( GL.QUADS );
            GL.Color( color );
        }

        public static void DrawQuads(Rect rect)
        {
            Vector2 top_right = new Vector2() {
                x = rect.x + rect.width - 1,
                y = rect.y + rect.height - 1,
            };

            GL.Vertex3( rect.x, top_right.y, 0 );
            GL.Vertex3( top_right.x, top_right.y, 0 );
            GL.Vertex3( top_right.x, rect.y, 0 );
            GL.Vertex3( rect.x, rect.y, 0 );
        }

        public static void EndQuads()
        {
            GL.End();
            GL.PopMatrix();
        }
    }
}
